package cn.itcast.controller;

import cn.itcast.domain.Course;
import cn.itcast.service.CourseService;
import cn.itcast.service.impl.CourseServiceImpl;
import cn.itcast.utils.BeanUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/course/*")
public class CourseServlet extends BaseServlet {
    private CourseService courseService = new CourseServiceImpl();

    // TODO: 时间   ********  查询所有分页功能  *********
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取当前页
        String page = req.getParameter("page");
        int pageNum = 1;
        if (StringUtils.isNoneBlank(page)) {
            pageNum = Integer.parseInt(page);
        }
        List<Course> courseList = courseService.findAll(pageNum);

        PageInfo<Course> pageInfo = new PageInfo<Course>(courseList);

        req.setAttribute("page", pageInfo);

        req.getRequestDispatcher("/pages/course/list.jsp").forward(req, resp);


    }

    // TODO: 时间   ********  跳转新增页功能  *********
    protected void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.sendRedirect(req.getContextPath() + "/pages/course/add.jsp");
    }

    // TODO: 时间   ********  新增功能  *********
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Course course = BeanUtil.fillBean(req, Course.class);

        courseService.add(course);

        req.getRequestDispatcher("/store/course/findAll").forward(req,resp);
    }

    // TODO: 时间   ********  数据回显功能  *********
    protected void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        System.out.println(id+"2222222222222");
        Course course = courseService.findById(id);
        System.out.println(course);
        req.setAttribute("course",course);
        req.getRequestDispatcher("/pages/course/update.jsp").forward(req,resp);
    }
    // TODO: 时间   ********  修改功能  *********
    protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Course course = BeanUtil.fillBean(req, Course.class);
        courseService.edit(course);

        req.getRequestDispatcher("/store/course/findAll").forward(req,resp);
    }
    // TODO: 时间   ********  批量删除  *********
    protected void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
        String ids = req.getParameter("ids");

        courseService.deleteByIds(ids);


        resp.sendRedirect(req.getContextPath() + "/store/course/findAll");
    }
}
